﻿namespace ICMRemoteEdu
{
    using ICMRemoteEdu.Classes;
    using System;
    using System.Windows.Forms;

    public static class ICMTreeManager
    {
        public static void RefreshEditTree(TreeView tree, ICMCourse course)
        {
            tree.Nodes.Clear();
            tree.Nodes.Add(course.GetHashCode().ToString(), course.Text);
            if ((course.Partitions != null) && (course.Partitions.Count > 0))
            {
                foreach (ICMPartition partition in course.Partitions)
                {
                    tree.Nodes[course.GetHashCode().ToString()].Nodes.Add(partition.GetHashCode().ToString(), partition.Text);
                    if ((partition.Lessons != null) && (partition.Lessons.Count > 0))
                    {
                        foreach (ICMLesson lesson in partition.Lessons)
                        {
                            tree.Nodes.Find(partition.GetHashCode().ToString(), true)[0].Nodes.Add(lesson.GetHashCode().ToString(), lesson.Text);
                        }
                        continue;
                    }
                }
            }
            tree.ExpandAll();
            if (course.Questions.Count > 0)
            {
                TreeNode node = new TreeNode("Вопросы");
                tree.Nodes[course.GetHashCode().ToString()].Nodes.Add(node);
                foreach (ICMQuestion question in course.Questions)
                {
                    node.Nodes.Add(question.GetHashCode().ToString(), question.Text);
                }
            }
            tree.ExpandAll();
        }

        public static void RefreshViewTree(TreeView tree, ICMCourse course)
        {
            tree.Nodes.Clear();
            tree.Nodes.Add(course.GetHashCode().ToString(), course.Text);
            if ((course.Partitions != null) && (course.Partitions.Count > 0))
            {
                foreach (ICMPartition partition in course.Partitions)
                {
                    tree.Nodes[course.GetHashCode().ToString()].Nodes.Add(partition.GetHashCode().ToString(), partition.Text);
                    tree.Nodes.Find(partition.GetHashCode().ToString(), true)[0].StateImageIndex = 2;
                    if ((partition.Lessons != null) && (partition.Lessons.Count > 0))
                    {
                        foreach (ICMLesson lesson in partition.Lessons)
                        {
                            tree.Nodes.Find(partition.GetHashCode().ToString(), true)[0].Nodes.Add(lesson.GetHashCode().ToString(), lesson.Text);
                            tree.Nodes.Find(lesson.GetHashCode().ToString(), true)[0].StateImageIndex = 1;
                        }
                        continue;
                    }
                }
            }
            tree.ExpandAll();
        }
    }
}

